package view.main;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import model.world.CSMap;

import constants.Icons;
import constants.MapFiles;
import constants.Maps;






public class Starter extends JFrame implements ActionListener, WindowListener {

	private static final String[] list = {"de_dust2",
		"de_inferno",
		"de_nuke",
		"de_train",
		"de_cbble",};
	
	private JComboBox maps;
	private JButton ok;
	
	private MainThread thread;
	
	public Starter(MainThread thread_) {
		thread = thread_;
		maps = new JComboBox(list);
		maps.setMaximumSize(new Dimension(100,25));
		maps.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		ok = new JButton("Ok");
		ok.setActionCommand("ok");
		
		ok.addActionListener(this);
		ok.setAlignmentX(Component.CENTER_ALIGNMENT);
		JPanel main = new JPanel();
		main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
		
		JLabel label = new JLabel(Icons.getIcon("smallImage"));
		label.setBorder(BorderFactory.createLoweredBevelBorder());
		label.setAlignmentX(Component.CENTER_ALIGNMENT);
		main.add(label);
		main.add(Box.createVerticalStrut(5));
		main.add(maps);
		main.add(Box.createVerticalStrut(5));
		main.add(ok);
		
		this.setTitle("StratTimer starter");
		this.setContentPane(main);
		this.addWindowListener(this);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
	}

	public void display() {
		this.pack();
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(screen.width/2-this.getWidth()/2, screen.height/2-this.getHeight()/2);
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("ok")) {
			switch (maps.getSelectedIndex()) {
			case 0:
				thread.setMap(new CSMap(Maps.getMap("de_dust2"),MapFiles.getMapFile("de_dust2")));
				break;
			case 1:
				thread.setMap(new CSMap(Maps.getMap("de_inferno"),MapFiles.getMapFile("de_inferno")));
				break;
			case 2:
				thread.setMap(new CSMap(Maps.getMap("de_nuke"),MapFiles.getMapFile("de_nuke")));
				break;
			case 3:
				thread.setMap(new CSMap(Maps.getMap("de_train"),MapFiles.getMapFile("de_train")));
				break;
			case 4:
				thread.setMap(new CSMap(Maps.getMap("de_cbble"),MapFiles.getMapFile("de_cbble")));
				break;
			default:
				break;
			}
			this.dispose();
		}
	}

	public void windowActivated(WindowEvent arg0) {}
	public void windowClosed(WindowEvent arg0) {}

	public void windowClosing(WindowEvent arg0) {
		thread.setMap(null);
		this.dispose();
	}

	public void windowDeactivated(WindowEvent arg0) {}
	public void windowDeiconified(WindowEvent arg0) {}
	public void windowIconified(WindowEvent arg0) {}
	public void windowOpened(WindowEvent arg0) {}
}
